﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package org.aswing {
    import flash.events.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.utils.*;
    import org.aswing.geom.*;
    import org.aswing.event.*;
    import org.aswing.util.*;
    import org.aswing.plaf.basic.*;

    public class JToolTip extends Container {

        private static const WAIT_TIME:int = 600;
        private static const FAST_OCCUR_TIME:int = 50;

        private static var last_tip_dropped_time:int = 0;
        private static var defaultRoot:DisplayObjectContainer = null;

        private var containerRoot:DisplayObjectContainer;
        private var tipText:String;
        private var comp:InteractiveObject;
        private var offsets:IntPoint;
        private var offsetsRelatedToMouse:Boolean;
        private var timer:Timer;
        private var waitThenPopupEnabled:Boolean;

        public function JToolTip(){
            setName("JToolTip");
            this.offsets = new IntPoint(4, 20);
            this.offsetsRelatedToMouse = true;
            this.waitThenPopupEnabled = true;
            this.timer = new Timer(WAIT_TIME, false);
            this.timer.setInitialDelay(WAIT_TIME);
            this.timer.addActionListener(this.__timeOnAction);
            mouseEnabled = false;
            mouseChildren = false;
            this.updateUI();
        }
        public static function setDefaultToolTipContainerRoot(_arg1:DisplayObjectContainer):void{
            if (_arg1 != defaultRoot){
                defaultRoot = _arg1;
            };
        }
        private static function getDefaultToolTipContainerRoot():DisplayObjectContainer{
            if (defaultRoot == null){
                return (AsWingManager.getRoot());
            };
            return (defaultRoot);
        }

        public function setToolTipContainerRoot(_arg1:DisplayObjectContainer):void{
            if (_arg1 != this.containerRoot){
                this.containerRoot = _arg1;
            };
        }
        private function getToolTipContainerRoot():DisplayObjectContainer{
            var _local1:DisplayObjectContainer;
            if (this.containerRoot == null){
                if (this.getTargetComponent() != null){
                    _local1 = (this.getTargetComponent().root as DisplayObjectContainer);
                };
                if (_local1 == null){
                    _local1 = getDefaultToolTipContainerRoot();
                };
                return (_local1);
            };
            return (this.containerRoot);
        }
        override public function updateUI():void{
            setUI(UIManager.getUI(this));
        }
        override public function getDefaultBasicUIClass():Class{
            return (BasicToolTipUI);
        }
        override public function getUIClassID():String{
            return ("ToolTipUI");
        }
        public function startWaitToPopup():void{
            if ((getTimer() - last_tip_dropped_time) < FAST_OCCUR_TIME){
                this.timer.setInitialDelay(FAST_OCCUR_TIME);
            } else {
                this.timer.setInitialDelay(WAIT_TIME);
            };
            this.timer.restart();
            if (this.getTargetComponent()){
                this.getTargetComponent().addEventListener(MouseEvent.MOUSE_MOVE, this.__onMouseMoved, false, 0, true);
            };
        }
        public function stopWaitToPopup():void{
            this.timer.stop();
            if (this.getTargetComponent()){
                this.getTargetComponent().removeEventListener(MouseEvent.MOUSE_MOVE, this.__onMouseMoved);
            };
            last_tip_dropped_time = getTimer();
        }
        public function setWaitThenPopupEnabled(_arg1:Boolean):void{
            this.waitThenPopupEnabled = _arg1;
        }
        public function isWaitThenPopupEnabled():Boolean{
            return (this.waitThenPopupEnabled);
        }
        protected function __compRollOver(_arg1:InteractiveObject):void{
            if ((((_arg1 == this.comp)) && (this.isWaitThenPopupEnabled()))){
                this.startWaitToPopup();
            };
        }
        protected function __compRollOut(_arg1:InteractiveObject):void{
            if ((((_arg1 == this.comp)) && (this.isWaitThenPopupEnabled()))){
                this.disposeToolTip();
            };
        }
        private function __onMouseMoved(_arg1:Event):void{
            if (this.timer.isRunning()){
                this.timer.restart();
            };
        }
        private function __timeOnAction(_arg1:Event):void{
            this.timer.stop();
            dispatchEvent(new ToolTipEvent(ToolTipEvent.TIP_SHOWING));
            this.disposeToolTip();
            this.viewToolTip();
        }
        private function viewToolTip():void{
            var _local3:Point;
            if (this.tipText == null){
                return;
            };
            var _local1:DisplayObjectContainer = this.getToolTipContainerRoot();
            if (_local1 == null){
                trace("getToolTipContainerRoot null");
                return;
            };
            _local1.addChild(this);
            var _local2:IntPoint = new IntPoint();
            if (this.offsetsRelatedToMouse){
                _local3 = _local1.localToGlobal(new Point(_local1.mouseX, _local1.mouseY));
                _local2.setWithPoint(_local3);
            } else {
                _local2.setWithPoint(this.getTargetComponent().localToGlobal(new Point(0, 0)));
            };
            this.moveLocationRelatedTo(_local2);
        }
        public function moveLocationRelatedTo(_arg1:IntPoint):void{
            if (!isShowing()){
                return;
            };
            _arg1 = _arg1.clone();
            _arg1.move(this.offsets.x, this.offsets.y);
            var _local2:IntDimension = getPreferredSize();
            var _local3:IntRectangle = AsWingUtils.getVisibleMaximizedBounds(parent);
            if ((_arg1.x + _local2.width) > (_local3.x + _local3.width)){
                _arg1.x = ((_local3.x + _local3.width) - _local2.width);
            };
            if ((_arg1.y + _local2.height) > (_local3.y + _local3.height)){
                _arg1.y = ((_local3.y + _local3.height) - _local2.height);
            };
            if (_arg1.x < _local3.x){
                _arg1.x = _local3.x;
            };
            if (_arg1.y < _local3.y){
                _arg1.y = _local3.y;
            };
            setGlobalLocation(_arg1);
            setSize(_local2);
            revalidate();
        }
        public function showToolTip():void{
            this.viewToolTip();
        }
        public function disposeToolTip():void{
            this.stopWaitToPopup();
            removeFromContainer();
        }
        public function setTipText(_arg1:String):void{
            if (_arg1 != this.tipText){
                this.tipText = _arg1;
                dispatchEvent(new ToolTipEvent(ToolTipEvent.TIP_TEXT_CHANGED));
                if (_arg1 == null){
                    this.disposeToolTip();
                } else {
                    if (isShowing()){
                        setSize(getPreferredSize());
                        repaint();
                        revalidate();
                    };
                };
            };
        }
        public function getTipText():String{
            return (this.tipText);
        }
        public function setTargetComponent(_arg1:InteractiveObject):void{
            if (_arg1 != this.comp){
                if (this.comp != null){
                    this.unlistenOwner(this.comp);
                };
                this.comp = _arg1;
                if (this.comp != null){
                    this.listenOwner(this.comp);
                };
            };
        }
        public function getTargetComponent():InteractiveObject{
            return (this.comp);
        }
        public function setOffsets(_arg1:IntPoint):void{
            this.offsets.setLocation(_arg1);
        }
        public function getOffsets():IntPoint{
            return (this.offsets.clone());
        }
        public function setOffsetsRelatedToMouse(_arg1:Boolean):void{
            this.offsetsRelatedToMouse = _arg1;
        }
        public function isOffsetsRelatedToMouse():Boolean{
            return (this.offsetsRelatedToMouse);
        }
        protected function listenOwner(_arg1:InteractiveObject, _arg2:Boolean=false):void{
            _arg1.addEventListener(MouseEvent.ROLL_OVER, this.____compRollOver, false, 0, _arg2);
            _arg1.addEventListener(MouseEvent.ROLL_OUT, this.____compRollOut, false, 0, _arg2);
            _arg1.addEventListener(MouseEvent.MOUSE_DOWN, this.____compRollOut, false, 0, _arg2);
        }
        protected function unlistenOwner(_arg1:InteractiveObject):void{
            _arg1.removeEventListener(MouseEvent.ROLL_OVER, this.____compRollOver);
            _arg1.removeEventListener(MouseEvent.ROLL_OUT, this.____compRollOut);
            _arg1.removeEventListener(MouseEvent.MOUSE_DOWN, this.____compRollOut);
            _arg1.removeEventListener(MouseEvent.MOUSE_MOVE, this.__onMouseMoved);
        }
        private function ____compRollOver(_arg1:Event):void{
            var _local2:InteractiveObject = (_arg1.currentTarget as InteractiveObject);
            this.__compRollOver(_local2);
        }
        private function ____compRollOut(_arg1:Event):void{
            var _local2:InteractiveObject = (_arg1.currentTarget as InteractiveObject);
            this.__compRollOut(_local2);
        }

    }
}//package org.aswing 
